<?php

namespace Home\Controller;

/**
 * Description of HotelApplyController.class控制器实例
 * 
 * @author zhangzhen
 * @date 2017-1-4
 */
class DealerController extends ParentController {

    /**
     * @description 经销商申请列表视图页面
     * @modify zhangzhen 2017-01-03
     */
    public function index() {
        $param = I('post.');
        $where = $condition = array();
        if (isset($param['k']) && $param['k']) {
            $condition['k'] = $param['k'];
            $where['_string'] = "name like '%" . $param['k'] . "%' OR mobile like '%" . $param['k'] . "%'";
        }

        $dealerApplyModel = D('DealerApply');
        $count = $dealerApplyModel->where($where)->count();
        $Page = classPage($count);
        $order = 'status asc,createtime desc';
        $data = $dealerApplyModel->where($where)->order($order)->limit($Page->firstRow, $Page->listRows)->select();
        
        foreach ($data as $key => &$value) {
            $value['idcardpath'] = MEIZHU_HOST. $value['idcardpath'];
        }
        $page = $Page->show();

        $this->assign('page', $page);
        $this->assign('condition', $condition);
        $this->assign('applyData', $data);
        $this->display();
    }

    /**
     * @description 经销商账号列表视图页面
     * @modify zhangzhen 2017-01-03
     */
    public function account() {
        $param = I('post.');
        $where = $condition = array();
        if (isset($param['k']) && $param['k']) {
            $condition['k'] = $param['k'];
            $where['_string'] = "name like '%" . $param['k'] . "%' OR mobile like '%" . $param['k'] . "%'";
        }

        $dealerLoginyModel = D('DealerLogin');
        $count = $dealerLoginyModel->where($where)->count();
        $Page = classPage($count);
        $order = 'status asc,createtime desc';
        $data = $dealerLoginyModel->where($where)->order($order)->limit($Page->firstRow, $Page->listRows)->select();
        
        foreach ($data as $key => &$value) {
            $value['idcardpath'] = $value['idcardpath'];
        }
        $page = $Page->show();

        $this->assign('page', $page);
        $this->assign('condition', $condition);
        $this->assign('dealerData', $data);
        $this->display();
    }

    /**
     * @description 经销商客栈申请列表视图页面
     * @modify zhangzhen 2017-01-05
     */
    public function hotel() {
        $param = I('post.');
        $where = $condition = array();
        if (isset($param['k']) && $param['k']) {
            $condition['k'] = $param['k'];
            $where['_string'] = "hotelname like '%" . $param['k'] . "%' OR mobile like '%" . $param['k'] . "%'";
        }
        $where['dealerlogin_id'] = array('neq',0);
        $hotelApplyModel = D('HotelApply');
        $count = $hotelApplyModel->where($where)->count();
        $Page = classPage($count);
        $order = 'status asc,createtime desc';
        $data = $hotelApplyModel
                    ->field('tb_hotel_apply.* , tb_dealer_login.name as dealername , tb_dealer_login.mobile as dealermobile, tb_dealer_login.enddate as enddate')
                    ->join('tb_dealer_login on tb_dealer_login.id = tb_hotel_apply.dealerlogin_id','LEFT')
                    ->where($where)
                    ->order($order)
                    ->limit($Page->firstRow, $Page->listRows)
                    ->select();
        $page = $Page->show();

        //获取所有省份数据
        $provinceModel = D('Province');
        $provinces = $provinceModel->getAll();
        foreach ($provinces as $key => &$provincesItem) {
           $provincesItem['name'] = explode('/', $provincesItem['name'])[0];
        }
        unset($provincesItem);

        //获取地区名称
        $districtModel = D('District');
        foreach ($data as $key => &$dataItem) {
           $disName = $districtModel->getName($dataItem['district_id']);
           $dataItem['districtName'] = !empty($disName) ? explode('/', $disName)[0] : '';
        }
        unset($dataItem);

        //获取区号时区数据
        $DealerApplyLogic = A('Common/DealerApply', 'Logic');
        $universalTimeData = $DealerApplyLogic->universalTimeList();
        $this->assign('universalTimeList', $universalTimeData);

        // 获取系统货币
        $currencyModel = D('Currency');
        $currencyData = $currencyModel->getCurrencyInfo();
        $this->assign('currencyInfo', $currencyData);

        $this->assign('page', $page);
        $this->assign('condition', $condition);
        $this->assign('applyData', $data);
        $this->assign('provinces', $provinces);
        $this->display();
    }

    /**
     * @description  更新为拒绝状态
     * @modify zhangzhen 2017-01-04
     */
    public function setStatus() {
        $applyId = I('post.id', 0, 'intval');
        $dealerApplyModel = D('DealerApply');
        $dealerApply = $dealerApplyModel->getInfo($applyId);
        if (empty($dealerApply)) {
            returnJson(403, 122, '数据不存在');
        }

        $result = $dealerApplyModel->setStatus($applyId, 2);
        if ($result == false) {
            returnJson(403, 123, '更新失败');
        }
        //发送拒绝短信通知
        if ($dealerApply['areacode'] == '86') {
            $sms = new \Org\Util\Sms();
            $sms->sendDealerRefuse($dealerApply['mobile']);
        } else {
            $sms = new \Org\Util\yunPian();
            $sms->sendDealerRefuse($dealerApply['areacode'], $dealerApply['mobile']);
        }
        
        returnJson(200, 200, '更新成功');
    }

    /**
     * @description  经销商申请开通服务
     * @modify zhangzhen 2017-01-04
     */
    public function handle() {
        $params = I('post.');
        $applyId = $params['id'];
        $userName = $params['name'];
        $mobile = $params['mobile'];
        $areacode = $params['areacode'];
        $enddate = $params['enddate'];
        $idcard = $params['idcard'];
        $idcardpath = $params['idcardpath'];
        $dealerApplyLogic = A('Common/DealerApply', 'Logic');
        $dealerApplyLogic->handle($applyId, $userName, $mobile, $enddate, $idcard, $idcardpath, $areacode);
        returnJson(200, 200, '开通成功');
    }
    /**
     * @description  经销商申请开通服务
     * @modify zhangzhen 2017-01-04
     */
    public function passDealerHotel() {
        $applyId = I('post.id', 0, 'intval');
        $hotelName = I('post.hotel', '', 'string');
        $mobile = I('post.mobile', '', 'string');
        $cityId = I('post.city', 0, 'intval');
        $districtId = I('post.district', 0, 'intval');
        $userName = I('post.username', '', 'string');
        $dealerId = I('post.dealerId', '', 'string');
        $enddate = I('post.enddate');
        $areaCode = I('post.areaCode', '86', 'string');
        $universaltimeId = I('post.universaltimeId', '5', 'string');
        $usage = I('post.usage', '1', 'string');
        $currencyId = I('post.currencyId', 0 , 'intval');

        $hotelApplyLogic = A('Common/HotelApply', 'Logic');
        $hotelApplyLogic->passDealerHotel($applyId, $mobile, $hotelName, $cityId, $districtId, $userName, $enddate ,$dealerId, $areaCode, $universaltimeId, $usage, $currencyId);
        returnJson(200, 200, '开通申请成功');
    }
    /**
     * @description 返回经销商客栈数据
     * @modify zhangzhen 2017-01-05
     */
    public function getHotel() {
        $dealerId = I('post.dealerId');
        $dealerAdminModel = D('DealerAdmin');
        $dealerAdmin = $dealerAdminModel->getInfo($dealerId);
        foreach ($dealerAdmin as $key => $value) {
            $adminIds[] = $value['adminlogin_id'];
        }
        $adminIds = implode(',', $adminIds);

        $adminHotelModel = D('AdminHotel');
        $adminHotels = $adminHotelModel->getInfosByAdmin($adminIds, 1);
       
        $hotelEntityModel = D('HotelEntity');
        $adminLoginModel = D('AdminLogin');
        $data = array();
        foreach ($adminHotels as $key => $adminHotel) {
            $data[$key] = $hotelEntityModel->getInfo($adminHotel['hotelentity_id']);
            $temp = $adminLoginModel->getInfo($adminHotel['adminlogin_id']);
            $data[$key]['username'] = $temp['name'];
        }

        //转化数据
        foreach ($data as &$row) {
            $row['cityname'] = D('City')->getName($row['city_id']);
            $row['areaname'] = D('District')->getName($row['district_id']);
        }


        $this->returnJson(200, 200, $data);
    }

    /**
     * @description 设置经销商到期时间
     * @modify zhangzhen 2017-1-11
     */
    public function setEndtime(){
        $params = I('post.');
        $dealerId = $params['dealerId'];
        $enddate = $params['enddate'];
        $dealerLoginModel = D('DealerLogin');
        $dealerLoginInfo = $dealerLoginModel->getInfo($dealerId);
        if (empty($enddate)) {
            $this->returnJson(200, 200,array('enddate' => $dealerLoginInfo['enddate'] ));
        }
        $data = array(
            'enddate' => $enddate
        );

        if ($enddate > $dealerLoginInfo['enddate']) {
            $dealerLoginModel->saveInfo($dealerId, $data);
            $this->returnJson(200, 200, $data);
        } else {
            $result = $dealerLoginModel->saveInfo($dealerId, $data);
            $dealerAdminModel = D('DealerAdmin');
            $dealerAdmin = $dealerAdminModel->getInfo($dealerId);
            foreach ($dealerAdmin as $key => $value) {
                $adminIds[] = $value['adminlogin_id'];
            }
            $adminIds = implode(',', $adminIds);

            $adminHotelModel = D('AdminHotel');
            $adminHotels = $adminHotelModel->getInfosByAdmin($adminIds, 1);
            $hotelEntityModel = D('HotelEntity');
            $adminLoginModel = D('AdminLogin');
            foreach ($adminHotels as $key => $adminHotel) {
                $hotelEntity = $hotelEntityModel->getInfo($adminHotel['hotelentity_id']);
                if( $hotelEntity['enddate'] > $enddate) {
                    $hotelEntityModel->setDate($adminHotel['hotelentity_id'],$enddate);
                }
            }
        }

        $this->returnJson(200, 200,$data);
    }

    /**
     * @description  更新为拒绝状态
     * @modify songanwei 2016-01-04
     */
    public function confuse() {
        $applyId = I('post.id', 0, 'intval');
        $hotelApplyModel = D('HotelApply');
        $hotelApply = $hotelApplyModel->getInfo($applyId);
        if (empty($hotelApply)) {
            returnJson(403, 122, '数据不存在');
        }

        $result = $hotelApplyModel->setStatus($applyId, 2);
        if ($result == false) {
            returnJson(403, 123, '更新失败');
        }
        //发送拒绝短信通知
       // $sms = new \Org\Util\Sms();
       // $sms->sendRefuse($hotelApply['mobile'], $hotelApply['hotelname']);
        returnJson(200, 200, '更新成功');
    }

    /**
     * @description  更新为拒绝状态
     * @modify songanwei 2016-01-04
     */
    public function getCountryProvince() {
        $param = I('post.');

        //获取所有省份数据
        $provinceModel = D('Province');
        $provinces = $provinceModel->getAllByCountry($param['countryId']);
        foreach ($provinces as $key => &$provincesItem) {
           $provincesItem['name'] = explode('/', $provincesItem['name'])[0];
        }
        unset($provincesItem);
        returnJson(200, 200, $provinces);
    }

}
